<?php
/*******************************************************************
 *[TTTuangou] (C)2005 - 2010 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename order.logic.php $
 *
 * @Author http://www.tttuangou.net $
 *
 * @Date 2011-03-28 08:07:47 $
 *******************************************************************/ 
 


class OrderLogic{
	var $DatabaseHandler;
	var $Config;
	
	function OrderLogic(){
		$this->DatabaseHandler = &Obj::registry("DatabaseHandler");
		$this->Config = &Obj::registry("config");
	}
	function orderGet($orderid,$uid='',$alls=false){
		if ($alls)
		{
			$status_condition = '1';
		}
		else
		{
			$status_condition = 'status = 1';
		}
		$orderid = (is_numeric($orderid) ? $orderid : 0);
		$sql='select * from '.TABLE_PREFIX.'tttuangou_order where orderid='.$orderid.' and '.$status_condition;
		$uid = (is_numeric($uid) ? $uid : 0);
		if($uid!='') $sql.=' and userid = '.$uid;

		$query = $this->DatabaseHandler->Query($sql);
		$order=$query->GetRow();
		return $order;
	}
	function GetOrderId(){
		$id=date('Ymd',time()).str_pad(rand('1','99999'),5,'0',STR_PAD_LEFT);
		$sql='select * from '.TABLE_PREFIX.'tttuangou_order where orderid = '.$id;
		$query = $this->DatabaseHandler->Query($sql);
		$order=$query->GetRow();
		if(empty($order)){
			return $id;
		}
		$this->GetOrderId();
	}
	function GetOrderList($user,$pay=9,$status=''){
		$page=floatval($_REQUEST['page'])==false?1:floatval($_REQUEST['page']);
		if($pay==9){
			$sql='SELECT count(*) FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' order by o.buytime desc';
		}elseif($status!=''){
			$sql='SELECT count(*) FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' and o.pay='.floatval($pay).' and p.status='.floatval($status).' order by o.buytime desc';
		}else{
			$sql='SELECT count(*) FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' and o.pay='.floatval($pay).' order by o.buytime desc';
		}
		$query = $this->DatabaseHandler->Query($sql); 
		$num=$query->GetRow();
		$num=$num['count(*)'];
		$pagenum=10;		$page_arr = page($num,$pagenum,$query_link,$_config);
		if($pay==9){
			$sql='SELECT o.*,p.type,p.name,p.nowprice,p.intro,p.img FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' order by o.buytime desc limit  '.($page-1)*$pagenum.','.$pagenum;
		}elseif($status!=''){
			$sql='SELECT o.*,p.type,p.name,p.nowprice,p.intro,p.img FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' and o.pay='.floatval($pay).' and o.status='.floatval($status).' order by o.buytime desc limit  '.($page-1)*$pagenum.','.$pagenum;
		}else{
			$sql='SELECT o.*,p.type,p.name,p.nowprice,p.intro,p.img FROM '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id where o.userid = '.floatval($user) .' and o.pay='.floatval($pay).' order by o.buytime desc limit  '.($page-1)*$pagenum.','.$pagenum;
		}
		$query = $this->DatabaseHandler->Query($sql); 
		$order=$query->GetAll();
		Load::logic('me');
		$MeLogic = new MeLogic();
		foreach ($order as $i => $value){
			$order[$i]['price']=$value['productnum']*$value['nowprice']+$value['expressprice'];
			$order[$i]['time']=$value['buytime'];
		};
		$order['page_arr']=$page_arr;
		return $order;
	}
	function orderCheck($id){
		$id = (is_numeric($id) ? $id : 0);
		$sql='select * from '.TABLE_PREFIX.'tttuangou_order where orderid='.$id.' and status=1 and pay=0';
		$query = $this->DatabaseHandler->Query($sql);
		$order=$query->GetRow();
		return $order;
	}
	function orderGetByUser($productid,$uid,$checkMuti=false){
		if (false == $checkMuti)
		{
			$sql='select * from '.TABLE_PREFIX.'tttuangou_order where productid='.floatval($productid).' and userid='.floatval($uid).' and status=1';
		}
		else
		{
			$sql='select o.*,p.mutibuy from '.TABLE_PREFIX.'tttuangou_order o LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p ON(o.productid=p.id) where o.productid='.floatval($productid).' and o.userid='.floatval($uid).'';
		}
		$query = $this->DatabaseHandler->Query($sql);
		if (!$query) return '';
		$order=$query->GetRow();
		if ($order['mutibuy']!='' && $order['mutibuy']=='true')
		{
						return '';
		}
		return $order;
	}
	function orderEdit($orderid,$ary=array()){
		$orderid = (is_numeric($orderid) ? $orderid : 0);
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_order');
		$result=$this->DatabaseHandler->Update($ary,'orderid='.$orderid);
		return $result;
	}
	function orderCreater($ary=array()){
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_order');
		$result=$this->DatabaseHandler->Insert($ary);
		return $result;
	}
	function orderType($id,$type,$paytime='',$pay=''){
		$ary=array(
			'status'=>$type,
	 	);
		if($paytime!='')$ary['paytime']=time();
		if($pay!=''){
			$ary['pay']=1;
			$sql='update '.TABLE_PREFIX.'tttuangou_product p left join '.TABLE_PREFIX.'tttuangou_order o on p.id=o.productid set p.totalnum = p.totalnum + 1 where o.orderid = '.$id;
			$this->DatabaseHandler->Query($sql);
		}
	 	$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_order');
		$id = (is_numeric($id) ? $id : 0);
		$result=$this->DatabaseHandler->Update($ary,'orderid='.$id);
		return $result;
	}
	
	function orderPaylist($productid){
		$sql='select o.*,m.username,m.email,p.nowprice,p.successnum from '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'system_members m on o.userid= m.uid left join '.TABLE_PREFIX.'tttuangou_product p on o.productid=p.id where o.productid = '.floatval($productid).' and o.pay = 1 and o.status = 1';
		$query = $this->DatabaseHandler->Query($sql);
		$orderPayed=$query->GetAll();
		return $orderPayed;
	}
	
		function listProductOrderPayed($productid)
	{
		$sql='select o.*,m.username,m.email,p.nowprice,p.successnum from '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'system_members m on o.userid= m.uid left join '.TABLE_PREFIX.'tttuangou_product p on o.productid=p.id where o.productid = '.floatval($productid).' and o.pay = 1';
		$query = $this->DatabaseHandler->Query($sql);
		$orderPayed=$query->GetAll();
		return $orderPayed;
	}

	
	function orderExpressUpdate($orderid, $expressid)
	{
		$ary = array(
			'expressid'=>$expressid,
			'expresstime'=>time(),
			'status'=>4 		);
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_order');
		$orderid = (is_numeric($orderid) ? $orderid : 0);
		$this->DatabaseHandler->Update($ary, 'orderid='.$orderid);
		return true;
	}

	function orderWaitExpressCount($productid=0, $sql_search='')
	{
		if ($productid>0)
		{
			$productid = (is_numeric($productid) ? $productid : 0);
			$limit = 'productid='.$productid;
		}
		else
		{
			$limit = 'status=1';
		}
		$sql='select count(orderid) AS count from '.TABLE_PREFIX.'tttuangou_order where pay = 1 and addressid <> 0 and '.$limit.' '.$sql_search;
		$query = $this->DatabaseHandler->Query($sql);
		$orderCount=$query->GetRow();
		return $orderCount['count'];
	}
	
	function orderSentExpressCount($productid=0, $sql_search='')
	{
		if ($productid>0)
		{
			$productid = (is_numeric($productid) ? $productid : 0);
			$limit = 'productid='.$productid;
		}
		else
		{
			$limit = 'status IN(4,9)';
		}
		$sql='select count(orderid) AS count from '.TABLE_PREFIX.'tttuangou_order where pay = 1 and addressid <> 0 and '.$limit.' '.$sql_search;
		$query = $this->DatabaseHandler->Query($sql);
		$orderCount=$query->GetRow();
		return $orderCount['count'];
	}

	function orderWaitExpressList($productid=0, $page=0, $epage=20, $sql_search='')
	{
		$condition = '1';
		if ($productid > 0)
		{
			$productid = (is_numeric($productid) ? $productid : 0);
			$condition = 'o.productid='.$productid;
		}
		$limit = '';
		if ($page > 0)
		{
			$limit = ' LIMIT '.((int)$page-1)*$epage.','.$epage;
		}
		$sql='SELECT p.name,p.successnum,o.orderid,o.addressid,m.username,o.paytime FROM '.TABLE_PREFIX.'tttuangou_order o LEFT JOIN '.TABLE_PREFIX.'system_members m ON m.uid=o.userid LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p ON p.id=o.productid WHERE p.type = "stuff" AND o.pay = 1 AND p.status IN(0,1,2) AND o.status = 1 AND '.$condition.' '.$sql_search.' ORDER BY o.paytime ASC'.$limit;
		return $this->DatabaseHandler->Query($sql)->GetAll();
	}

	function orderSentExpressList($productid=0, $page=0, $epage=20, $sql_search='')
	{
		$condition = '1';
		if ($productid > 0)
		{
			$productid = (is_numeric($productid) ? $productid : 0);
			$condition = 'o.productid='.$productid;
		}
		$limit = '';
		if ($page > 0)
		{
			$limit = ' LIMIT '.((int)$page-1)*$epage.','.$epage;
		}
		$sql='SELECT p.name,p.successnum,o.orderid,o.addressid,m.username,o.expresstime FROM '.TABLE_PREFIX.'tttuangou_order o LEFT JOIN '.TABLE_PREFIX.'system_members m ON m.uid=o.userid LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p ON p.id=o.productid WHERE p.type = "stuff" AND o.pay = 1 AND p.status IN(0,1,2) AND o.status IN(4,9) AND '.$condition.' '.$sql_search.' ORDER BY o.expresstime DESC'.$limit;
		return $this->DatabaseHandler->Query($sql)->GetAll();
	}

	function orderExpressConfirm($oid)
	{
		$ary = array(
			'status'=>9
		);
		$this->DatabaseHandler->SetTable(TABLE_PREFIX.'tttuangou_order');
		$oid = (is_numeric($oid) ? $oid : 0);
		return $this->DatabaseHandler->Update($ary, 'orderid='.$oid.' AND userid='.MEMBER_ID);
	}
	
	function questionlist(){
		$sql='select `id`,`content` from '.TABLE_PREFIX.'tttuangou_question where reply !="" order by time desc limit 0,6';
		$query = $this->DatabaseHandler->Query($sql);
		$question=$query->GetAll();
		return $question;
	}
	function UpOrder(){
		$sql='update '.TABLE_PREFIX.'tttuangou_order o left join '.TABLE_PREFIX.'tttuangou_product p on o.productid = p.id set o.status=2 where '.time().' > p.overtime and  o.pay = 0 and o.status = 1 and o.userid = '.MEMBER_ID;
		$query = $this->DatabaseHandler->Query($sql);
		return true;
	}
	function GetTicket($id,$seller=0){
		if($seller==0){
			$sql='select t.*,p.name,p.intro,p.nowprice,s.sellerurl,p.perioddate,s.selleraddress from '.TABLE_PREFIX.'tttuangou_ticket t LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p on t.productid = p.id left join '.TABLE_PREFIX.'tttuangou_seller s on p.sellerid=s.id where uid = '.MEMBER_ID .' and ticketid = '.floatval($id);	
		}else{
			$sql='select t.*,p.name,p.intro,p.nowprice,s.sellerurl,p.perioddate,s.selleraddress from '.TABLE_PREFIX.'tttuangou_ticket t LEFT JOIN '.TABLE_PREFIX.'tttuangou_product p on t.productid = p.id left join '.TABLE_PREFIX.'tttuangou_seller s on p.sellerid=s.id where ticketid = '.floatval($id);	
		}
		$query = $this->DatabaseHandler->Query($sql); 
		$ticket=$query->GetRow();
		return $ticket;
	}
}
?>